﻿Imports System.Web.UI
Imports System.Collections.Generic
Imports System.Reflection

Imports DotNetNuke
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Services.Localization

Namespace dnnWerk.Modules.Nuntio.Content

    Partial Class uc_Editor
        Inherits ContentBase

#Region "Delegates"

        Public Delegate Sub ContentenLoadedEventHandler(ByVal sender As Object, ByVal e As ContentChangedEventArgs)
        Public Delegate Sub ContentDeletedEventHandler(ByVal sender As Object, ByVal e As ContentChangedEventArgs)

#End Region

#Region "Events"

        Public Event ContentLoaded As ContentenLoadedEventHandler
        Public Event ContentDeleted As ContentDeletedEventHandler

#End Region

#Region "Private Members"

        Private _title As String = Null.NullString
        Private _content As String = Null.NullString
        Private _locale As String = Null.NullString
        Private _ispublished As Boolean = True
        Private _role As String = Null.NullString
        Private _moduleId As Integer
        Private _version As String
        Private _lcid As String

#End Region

#Region "Public Members"

        Public Overloads ReadOnly Property LocalResourceFile() As String
            Get
                Return Me.ModuleDirectory & "controls/App_LocalResources/uc_Editor.ascx"
            End Get
        End Property

        Public Property Version() As Integer
            Get
                Return _version
            End Get
            Set(ByVal value As Integer)
                _version = value
            End Set
        End Property

        Public Shadows Property ModuleId() As Integer
            Get
                Return _moduleid
            End Get
            Set(ByVal value As Integer)
                _moduleid = value
            End Set
        End Property

        Public Property Role() As String
            Get
                Return _role
            End Get
            Set(ByVal value As String)
                _role = value
            End Set
        End Property

        Public Property Title() As String
            Get
                Return _title
            End Get
            Set(ByVal value As String)
                _title = value
            End Set
        End Property

        Public ReadOnly Property SelectedTitle() As String
            Get
                Try
                    Return txtTitle.Text
                Catch
                End Try
                Return Null.NullString
            End Get
        End Property

        Public ReadOnly Property SelectedComment() As String
            Get
                Try
                    Return txtComment.Text
                Catch
                End Try
                Return Null.NullString
            End Get
        End Property

        Public ReadOnly Property SelectedContent() As String
            Get
                Try
                    Return ctlContent.Text
                Catch
                End Try
                Return Null.NullString
            End Get
        End Property

        Public ReadOnly Property ContentIsVisible() As Boolean
            Get
                Try
                    Return chkIsPublished.Checked
                Catch
                End Try
                Return False
            End Get
        End Property

        Public Property IsPublished() As Boolean
            Get
                Return _ispublished
            End Get
            Set(ByVal value As Boolean)
                _ispublished = value
            End Set
        End Property

        Public Property Content() As String
            Get
                Return _content
            End Get
            Set(ByVal value As String)
                _content = value
            End Set
        End Property

        Public Property Locale() As String
            Get
                Return _locale
            End Get
            Set(ByVal value As String)
                _locale = value
            End Set
        End Property

        Public Property LCID() As String
            Get
                Return _lcid
            End Get
            Set(ByVal value As String)
                _lcid = value
            End Set
        End Property

#End Region

#Region "Event Handlers"

        'Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        '    FixFCKAjax(ctlContent.GetType)
        'End Sub

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            LocalizeForm()

            If Not Page.IsPostBack Then

                BindData()

            End If


        End Sub

#End Region

#Region "Public Methods"

        Public Sub BindControl()

            BindData()

        End Sub

        Public Sub MatchContent(ByVal objContent As ContentInfo, ByRef ContentChanged As Boolean, ByRef AttributesChanged As Boolean)

            ContentChanged = False
            AttributesChanged = False

            If IsEmpty() Then
                ContentChanged = False
                AttributesChanged = False
                Exit Sub
            End If

            If objContent.Content <> Server.HtmlDecode(ctlContent.Text) _
                Or objContent.Title <> txtTitle.Text Then
                ContentChanged = True
            End If

            If objContent.IsPublished <> chkIsPublished.Checked Then
                AttributesChanged = True
            End If

        End Sub

        Private Function IsEmpty() As Boolean

            If Server.HtmlDecode(ctlContent.Text).ToLower = "<p>&#160;</p>" _
                Or Server.HtmlDecode(ctlContent.Text).ToLower = "<p>&nbsp;</p>" _
                Or Server.HtmlDecode(ctlContent.Text).ToLower = "<div>&#160;</div>" _
                Or Server.HtmlDecode(ctlContent.Text).ToLower = "<div>&nbsp;</div>" _
                Or Server.HtmlDecode(ctlContent.Text).ToLower = "&#160;" _
                Or Server.HtmlDecode(ctlContent.Text).ToLower = "&nbsp;" _
                Or Server.HtmlDecode(ctlContent.Text).ToLower.Trim = "" Then

                Return True

            End If

            Return False

        End Function

        Public Function GetDate(ByVal strDate As String) As String
            Dim dt As DateTime = Convert.ToDateTime(strDate)
            If dt.Date = Date.Now.Date Then
                Return String.Format(Localization.GetString("today", Me.LocalResourceFile), dt.ToShortTimeString)
            Else
                Return String.Format(Localization.GetString("date", Me.LocalResourceFile), dt.ToShortDateString, dt.ToShortTimeString)
            End If
        End Function

        Public Function GetComment(ByVal strComment As String, ByVal strUserComment As String) As String
            If strUserComment = "" Then
                Return Localization.GetString(strComment, Me.LocalResourceFile)
            Else
                Return strUserComment
            End If
        End Function

        Public Function GetVersionVisibility(ByVal strComment As String) As Boolean

            If strComment = Constants.CONTENT_MADE_HIDDEN Or strComment = Constants.CONTENT_MADE_VISIBLE Then
                Return False
            End If

            Return True

        End Function

        Public Function GetToolsVisibility(ByVal strVersion As String, ByVal strComment As String) As Boolean

            If strComment = Constants.CONTENT_MADE_HIDDEN Or strComment = Constants.CONTENT_MADE_VISIBLE Then
                Return False
            End If

            If Convert.ToInt32(strVersion) = Me.Version Then
                Return False
            End If

            Return True

        End Function

        Public Function GetRowCss(ByVal Version As String, ByVal strComment As String) As String

            If strComment = Constants.CONTENT_MADE_HIDDEN Or strComment = Constants.CONTENT_MADE_VISIBLE Then
                Return "nc_history_cell"
            End If

            If Convert.ToInt32(Version) = Me.Version Then
                Return "nc_history_active"
            Else
                Return "nc_history_cell"
            End If

        End Function

        Public Function GetUser(ByVal UserId As String) As String
            Dim id As Integer = Convert.ToInt32(UserId)
            Dim user As DotNetNuke.Entities.Users.UserInfo = DotNetNuke.Entities.Users.UserController.GetUser(PortalId, id, False)
            If Not user Is Nothing Then
                Return user.DisplayName
            Else
                Return "n/a"
            End If
        End Function

        Public Function GetText(ByVal strText As String) As String
            Return Localization.GetString(strText, Me.LocalResourceFile)
        End Function

#End Region

#Region "Private Methods"

        Private Sub LocalizeForm()
            lblComment.Text = Localization.GetString("lblComment", Me.LocalResourceFile)
            lblTitle.Text = Localization.GetString("lblTitle", Me.LocalResourceFile)
            chkIsPublished.Text = Localization.GetString("chkIsPublished", Me.LocalResourceFile)
            lblShowHistory.Text = Localization.GetString("lblShowHistory", Me.LocalResourceFile)
        End Sub

        Private Sub FixFCKAjax(ByVal TextEditorType As Type)

            If DotNetNuke.Framework.AJAX.IsInstalled() Then
                If TextEditorType.FullName.ToLower.Contains("fckhtmleditorprovider") Then
                    Dim sm As ScriptManager = ScriptManager.GetCurrent(Me.Page)
                    If Not sm Is Nothing Then
                        Dim strScript As String = "for ( var i = 0; i < parent.frames.length; ++i ) if ( parent.frames[i].FCK ) parent.frames[i].FCK.UpdateLinkedField();"
                        ScriptManager.RegisterOnSubmitStatement(Me.Page, Me.GetType(), "FCKAjaxHack", strScript)
                    End If
                End If
            End If

        End Sub

        Private Sub BindData()

            Try
                txtTitle.Text = Title
            Catch
            End Try

            Try
                ctlContent.Text = Content
            Catch
            End Try

            Try
                chkIsPublished.Checked = IsPublished
            Catch
            End Try

            Dim LogController As New ContentController
            Dim LogEntries As New List(Of ContentLogInfo)
            LogEntries = LogController.GetLogEntries(ModuleId, Locale, Role)

            If LogEntries.Count > 0 Then
                pnlHistory.Visible = True
                rptHistory.DataSource = LogEntries
                rptHistory.DataBind()
            Else
                pnlHistory.Visible = False
            End If

        End Sub

#End Region

        Public Sub cmdLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs)

            Dim args As String = CType(sender, LinkButton).CommandArgument
            Dim version As Integer = Convert.ToInt32(args.Split(Char.Parse("|"))(0))
            Dim locale As String = args.Split(Char.Parse("|"))(1)

            RaiseEvent ContentLoaded(Me, New ContentChangedEventArgs(version, Null.NullInteger, locale))

            'Dim lstContent As New ContentCollection
            'Dim ctl As New ContentController

            'lstContent = ctl.ListContent(ModuleId)
            'Dim objContent As ContentInfo = lstContent.FindItemById(id)

            'If Not objContent Is Nothing Then
            '    Version = objContent.Version
            '    Content = objContent.Content
            '    Title = objContent.Title
            '    IsPublished = objContent.IsPublished
            'End If

            'BindData()

        End Sub

        Public Sub cmdDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)

            Dim args As String = CType(sender, LinkButton).CommandArgument
            Dim id As Integer = Convert.ToInt32(args.Split(Char.Parse("|"))(0))
            Dim locale As String = args.Split(Char.Parse("|"))(1)

            RaiseEvent ContentDeleted(Me, New ContentChangedEventArgs(Null.NullInteger, id, locale))

            'Dim lstContent As New ContentCollection
            'Dim ctl As New ContentController

            'lstContent = ctl.ListContent(ModuleId)
            'Dim objContent As ContentInfo = lstContent.FindItemById(id)

            'If Not objContent Is Nothing Then
            '    ctl.DeleteContent(id)
            'End If

            'BindData()

        End Sub

    End Class

    Public Class ContentChangedEventArgs
        Private _newversion As Integer
        Private _contentid As Integer
        Private _locale As String
        Public Sub New(ByVal NewVersion As Integer, ByVal ContentId As Integer, ByVal Locale As String)
            _newversion = NewVersion
            _contentid = ContentId
            _locale = Locale
        End Sub
        Public Property Locale() As String
            Get
                Return _locale
            End Get
            Set(ByVal value As String)
                _locale = value
            End Set
        End Property
        Public Property ContentId() As Integer
            Get
                Return _contentid
            End Get
            Set(ByVal value As Integer)
                _contentid = value
            End Set
        End Property
        Public Property NewVersion() As Integer
            Get
                Return _newversion
            End Get
            Set(ByVal value As Integer)
                _newversion = value
            End Set
        End Property
    End Class

End Namespace
